package com.fishingwithme.domain.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.fishingwithme.infrastructure.mapper.UserCommentMapper;
import com.fishingwithme.infrastructure.utils.CommonUtils;

/**
 * 评论服务实现类
 */
@Service
public class UserCommentServiceImpl extends ServiceImpl<UserCommentMapper, UserComment> implements UserCommentService {
    @Autowired
    private LikeService likeService;

    @Override
    @Transactional
    public UserComment addComment(UserComment comment) {
        comment.setCreateTime(CommonUtils.now());
        if (comment.getLikeCount() == null) {
            comment.setLikeCount(0);
        }
        baseMapper.insert(comment);
        return comment;
    }

    @Override
    @Transactional
    public boolean deleteComment(Long commentId, Long userId) {
        UserComment comment = baseMapper.selectById(commentId);
        if (comment == null || !comment.getUserId().equals(userId)) {
            return false;
        }
        return baseMapper.deleteById(commentId) > 0;
    }

    @Override
    @Transactional
    public boolean updateCommentContent(Long commentId, Long userId, String content) {
        return baseMapper.updateContent(commentId, userId, content) > 0;
    }

    @Override
    public IPage<UserComment> getTopLevelComments(String targetType, Long targetId, int pageNum, int pageSize) {
        Page<UserComment> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectTopLevelComments(page, targetType, targetId,CommonUtils.getUserId());
    }

    @Override
    public IPage<UserComment> getCommentsWithHierarchy(String targetType, Long targetId, int pageNum, int pageSize) {
        return getTopLevelComments(targetType, targetId, pageNum, pageSize);
    }

    @Override
    public IPage<UserComment> getChildCommentsByPage(Long parentId, int pageNum, int pageSize) {
        Page<UserComment> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectChildCommentsByPage(page, parentId);
    }

    @Override
    public UserComment getCommentById(Long commentId) {
        return baseMapper.selectById(commentId);
    }

    @Override
    public IPage<UserComment> getUserComments(Long userId, int pageNum, int pageSize) {
        Page<UserComment> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectByUserId(page, userId);
    }

    @Override
    @Transactional
    public Boolean likeComment(Long commentId) {
        boolean success = likeService.likes(UserComment.LIKE_COMMENTS, commentId);
        baseMapper.updateCommentLikeCnt(UserComment.LIKE_COMMENTS,commentId);
        return success;
    }

    @Override
    @Transactional
    public Boolean unlikeComment(Long commentId) {
        boolean success = likeService.dislike(UserComment.LIKE_COMMENTS, commentId);
        baseMapper.updateCommentLikeCnt(UserComment.LIKE_COMMENTS,commentId);
        return success;
    }
}